﻿using System;
using System.Linq;
using SlamCms.Common;
using SlamCms.Configuration;

namespace SlamCms.Data
{
	public static class ContentItemExtensions
	{
		public static bool BelongsToZone(this ContentItem contentItem, params string[] zoneIds)
		{
			Ensure.ArgumentNotNull(contentItem, "contentItem");
			Ensure.ArgumentNotNull(zoneIds, "zoneIds");
			Ensure.ArgumentNotNull(contentItem.List, "contentItem", "The List property is null.");
			Ensure.ArgumentNotNull(contentItem.List.Site, "contentItem", "The Site property of the List is null");
			Ensure.ArgumentNotNull(contentItem.List.Site.Zones, "contentItem", "The Zones property of the Site is null");

			return contentItem.List.Site.Zones.Any(z => z.ZoneId.In(zoneIds));
		}

		public static bool BelongsToSite(this ContentItem contentItem, params Guid[] siteIds)
		{
			Ensure.ArgumentNotNull(contentItem, "contentItem");
			Ensure.ArgumentNotNull(siteIds, "siteIds");
			Ensure.ArgumentNotNull(contentItem.List, "contentItem", "The List property is null.");
			
			return contentItem.List.SiteId.In(siteIds);
		}

		public static bool BelongsToList(this ContentItem contentItem, params Guid[] listIds)
		{
			Ensure.ArgumentNotNull(contentItem, "contentItem");
			Ensure.ArgumentNotNull(listIds, "listIds");

			return contentItem.ListId.In(listIds);
		}

		public static bool IsDraft(this ContentItem contentItem)
		{
			Ensure.ArgumentNotNull(contentItem, "contentItem");

			return contentItem.StatusID.In(ContentItemStatus.New, ContentItemStatus.Modified, ContentItemStatus.PendingApproval, ContentItemStatus.ModifiedPendingApproval);
		}

		public static string GetLibraryUrl(this ContentItem contentItem)
		{
			Ensure.ArgumentNotNull(contentItem, "contentItem");
			Ensure.ArgumentNotNull(contentItem.List, "contentItem", "The List property is null.");
			Ensure.ArgumentNotNull(contentItem.List.Site, "contentItem", "The Site property of the List is null");
			Ensure.ArgumentNotNull(contentItem.List.Site.SiteCollection, "contentItem", "The SiteCollection property of the Site is null");

			var adminUrl = ConfigurationManager.Environment.DefaultApplication.AdminUrl;

			return adminUrl + contentItem.List.Site.SiteCollection.ServerRelativeUrl + contentItem.List.Site.Url + contentItem.List.DefaultViewUrl;
		}
	}
}
